home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 11255 / 11255.xpi / chrome / content / controller / buttons / ButtonEffectHelper.js next >
Text File  |  2009-11-25  |  9KB  |  261 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * 
  3.  * Pearltrees add-on AMO, Copyright(C), 2009, Broceliand SAS, Paris, France 
  4.  * (company in charge of developing Pearltrees)
  5.  * 
  6.  * This file is part of ΓÇ£Pearltrees add-on AMOΓÇ¥.  
  7.  * 
  8.  * Pearltrees add-on AMO is free software: you can redistribute it and/or modify it under the 
  9.  * terms of the GNU General Public License version 3 as published by the Free Software Foundation.
  10.  * 
  11.  * Pearltrees add-on AMO is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
  12.  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
  13.  * See the GNU General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License along with Pearltrees add-on AMO. 
  16.  * If not, see <http://www.gnu.org/licenses/>
  17.  * 
  18.  * ***** END LICENSE BLOCK ***** */
  19.  
  20. // ///////////////////////////////////////////////////////////////////////////////
  21. // Button Effect Helpers
  22. // ///////////////////////////////////////////////////////////////////////////////
  23.  
  24. var BRO_buttonEffectHelper = {
  25.  
  26.     START_RECORDING_EFFECT_TIME : 1320,
  27.     STOP_RECORDING_EFFECT_TIME : 300,
  28.     REVEAL_EFFECT_TIME : 1900,
  29.  
  30.     HELP_EFFECT_TIME : 900, // total effect time
  31.     HELP_TIME_HIDDEN : 300, // included in HELP_EFFECT_TIME
  32.     HELP_TIME_BEFORE_LOOP : 300, // pause before loop
  33.     HELP_OPACITY_STEPS : 0.1, // change opacity 0.1 by 0.1
  34.     HELP_MAX_OPACITY : 1, // css property
  35.  
  36.     _isRunningHelpEffects : false,
  37.     _runningEffects : [],
  38.  
  39.     runHelpEffects : function() {
  40.         this._isRunningHelpEffects = true;
  41.         this.runToolbarButtonHelpEffect('BRO_recordButton');
  42.         this.runToolbarButtonHelpEffect('BRO_newButton');
  43.         this.runToolbarButtonHelpEffect('BRO_homeButton');
  44.     },
  45.     stopHelpEffects : function() {
  46.         this._isRunningHelpEffects = false;
  47.     },
  48.  
  49.     stopIsRecordingEffect : function() {
  50.         this.stopToolbarButtonEffect('BRO_recordButton');
  51.         BRO_buttonEffectHelper.setButtonClass('BRO_recordButton',
  52.                 'BRO_recordButtonDefault');
  53.     },
  54.  
  55.     runIsRecordingEffect : function() {
  56.         var recordButtonEffect = new BRO_buttonEffect("BRO_recordButton",
  57.                 "BRO_recordButtonEffect", [1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1],
  58.                 "BRO_recordButtonIsRecording", 120);
  59.         this.runToolbarButtonEffect(recordButtonEffect);
  60.     },
  61.  
  62.     runRevealEffect : function() {
  63.         var homeButtonEffect = new BRO_buttonEffect("BRO_homeButton",
  64.                 "BRO_homeButtonEffect", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 9, 4, 3, 2, 1], "BRO_homeButton", 120);
  65.         this.runToolbarButtonEffect(homeButtonEffect);
  66.     },
  67.  
  68.     runToolbarButtonEffect : function(newEffect) {
  69.         if (!newEffect)
  70.             return;
  71.         var buttonId = newEffect.getButtonId();
  72.         var startingIndex = 0;
  73.  
  74.         // If there is already an active effect on this button
  75.         if (this.isButtonRunningEffect(buttonId)) {
  76.             var currentEffect = this.getButtonRunningEffect(buttonId);
  77.             // If the current running effect is using the same className
  78.             // then we will try to merge effects
  79.             if (currentEffect.getClassName() == newEffect.getClassName()) {
  80.                 startingIndex = this.getSyncIndex(currentEffect, newEffect);
  81.             }
  82.             // We stop the current effect
  83.             currentEffect.stop();
  84.         }
  85.         // We register the new effect as the current effect
  86.         this.setButtonRunningEffect(buttonId, newEffect);
  87.         newEffect.start(startingIndex);
  88.     },
  89.  
  90.     runToolbarButtonHelpEffect : function(buttonId) {
  91.         if (!this._isRunningHelpEffects)
  92.             return;
  93.         var button = document.getElementById(buttonId);
  94.         var image = this.getButtonImage(buttonId);
  95.         if (!button || !image)
  96.             return;
  97.  
  98.         var effectTime = this.HELP_EFFECT_TIME; // ms
  99.         var opacitySteps = this.HELP_OPACITY_STEPS;
  100.         var maxOpacity = this.HELP_MAX_OPACITY;
  101.         var stepsNum = maxOpacity / opacitySteps; // Thus we adjust opacity
  102.                                                     // 0.1 by 0.1
  103.         var timeBetweenSteps = effectTime / 2 / stepsNum;
  104.         for (var i = 1; i < stepsNum + 1; i++) {
  105.             var opacity = maxOpacity - (i * opacitySteps);
  106.             opacity = Math.round(opacity * 10) / 10;
  107.             var delay = i * timeBetweenSteps;
  108.             BRO_tools.callWithDelay("BRO_buttonEffectHelper.setButtonImageOpacity('"
  109.                             + buttonId + "','" + opacity + "')", delay);
  110.         }
  111.         for (var i = 1; i < stepsNum + 1; i++) {
  112.             var opacity = i * opacitySteps;
  113.             opacity = Math.round(opacity * 10) / 10;
  114.             var delay = this.HELP_TIME_HIDDEN + (effectTime / 2)
  115.                     + (i * timeBetweenSteps);
  116.             BRO_tools.callWithDelay("BRO_buttonEffectHelper.setButtonImageOpacity('"
  117.                             + buttonId + "','" + opacity + "')", delay);
  118.         }
  119.         var timeBeforeLoop = this.HELP_TIME_BEFORE_LOOP;
  120.         BRO_tools.callWithDelay("BRO_buttonEffectHelper.runToolbarButtonHelpEffect('"
  121.                         + buttonId + "')", effectTime + timeBeforeLoop);
  122.     },
  123.  
  124.     stopToolbarButtonEffect : function(buttonId) {
  125.         if (!this.isButtonRunningEffect(buttonId))
  126.             return;
  127.  
  128.         var currentEffect = this.getButtonRunningEffect(buttonId);
  129.         currentEffect.stop();
  130.     },
  131.  
  132.     getSyncIndex : function(currentEffect, newEffect) {
  133.         var currentClassNameIds = currentEffect.getClassNameIds();
  134.         var currentClassNameId = currentClassNameIds[currentEffect
  135.                 .getCurrentIndex()];
  136.         var newClassNameIds = newEffect.getClassNameIds();
  137.  
  138.         for (var i = 0; i < newClassNameIds.length; i++) {
  139.             if (newClassNameIds[i] == currentClassNameId) {
  140.                 return i;
  141.             }
  142.         }
  143.         return 0;
  144.     },
  145.  
  146.     isButtonRunningEffect : function(buttonId) {
  147.         return (this._runningEffects[buttonId] && this._runningEffects[buttonId]
  148.                 .isRunning());
  149.     },
  150.  
  151.     getButtonRunningEffect : function(buttonId) {
  152.         return this._runningEffects[buttonId];
  153.     },
  154.  
  155.     setButtonRunningEffect : function(buttonId, buttonEffect) {
  156.         this._runningEffects[buttonId] = buttonEffect;
  157.     },
  158.  
  159.     setButtonClass : function(buttonId, className) {
  160.         var button = document.getElementById(buttonId);
  161.         if (!button)
  162.             return;
  163.         button.className = className;
  164.     },
  165.  
  166.     getButtonImage : function(buttonId) {
  167.         var button = document.getElementById(buttonId);
  168.         return (button) ? button.boxObject.firstChild : null;
  169.  
  170.         /*
  171.          * @todo remove ? var children = button.boxObject.childNodes; var image =
  172.          * null; for(var i = 0; i < children.length; i++) { var tagName =
  173.          * children[i].tagName; if(tagName=="image" || tagName=="xul:image") {
  174.          * return child; } } return image;
  175.          */
  176.     },
  177.  
  178.     setButtonImageOpacity : function(buttonId, opacity) {
  179.         var image = this.getButtonImage(buttonId);
  180.         image.style.opacity = opacity;
  181.     }
  182. }
  183.  
  184. function BRO_buttonEffect(buttonId, className, classNameIds, endAnimationClassName, animationDelay, loopDelay) {
  185.     return {
  186.         _buttonId : buttonId,
  187.         _className : className,
  188.         _endAnimationClassName : endAnimationClassName,
  189.         _classNameIds : classNameIds,
  190.         _animationDelay : animationDelay,
  191.         _loopDelay : loopDelay,
  192.  
  193.         _currentIndex : 0,
  194.         _isRunning : false,
  195.  
  196.         getButtonId : function() {
  197.             return this._buttonId;
  198.         },
  199.  
  200.         getClassName : function() {
  201.             return this._className;
  202.         },
  203.  
  204.         getClassNameIds : function() {
  205.             return this._classNameIds;
  206.         },
  207.  
  208.         getCurrentIndex : function() {
  209.             return this._currentIndex;
  210.         },
  211.  
  212.         start : function(startingIndex) {
  213.             if (!startingIndex)
  214.                 startingIndex = 0;
  215.             this._currentIndex = startingIndex;
  216.             this._isRunning = true;
  217.             this.displayClass(this._classNameIds[this._currentIndex]);
  218.         },
  219.  
  220.         displayClass : function(classNameId) {
  221.             if (!this._isRunning)
  222.                 return;
  223.             BRO_buttonEffectHelper.setButtonClass(this._buttonId,
  224.                     this._className + classNameId);
  225.             this.displayNextClass();
  226.         },
  227.  
  228.         displayNextClass : function() {
  229.             // If it was the last class, we mark the effect as stoped.
  230.             if (!this._classNameIds[this._currentIndex + 1]) {
  231.                 BRO_buttonEffectHelper.setButtonClass(this._buttonId,
  232.                         this._endAnimationClassName);
  233.                 // If a loopDelay is specifed we will run the effect again
  234.                 if (this._loopDelay) {
  235.                     this._currentIndex = 0;
  236.                     BRO_tools.callWithDelay(function(thisObj) {
  237.                         thisObj.displayClass(thisObj._classNameIds[thisObj._currentIndex])
  238.                     }, this._loopDelay, this);
  239.                 } else {
  240.                     this.stop();
  241.                 }
  242.             }
  243.             // Else we will display the next class after the delay
  244.             else {
  245.                 this._currentIndex = this._currentIndex + 1;
  246.                 BRO_tools.callWithDelay(function(thisObj) {
  247.                     thisObj.displayClass(thisObj._classNameIds[thisObj._currentIndex])
  248.                 }, this._animationDelay, this);
  249.             }
  250.         },
  251.  
  252.         stop : function() {
  253.             this._isRunning = false;
  254.             this._loopDelay = null;
  255.         },
  256.  
  257.         isRunning : function() {
  258.             return this._isRunning;
  259.         }
  260.     }
  261. }